package com.skit.slidingviewandbottomsheet;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.slidingpanelayout.widget.SlidingPaneLayout;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;

/**
 * Author: shuike,
 * Email: shuike007@126.com,
 * Date: 2019/11/6.
 * PS:
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        log("onCreate");
        Window window = getWindow();
        window.getDecorView().setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);


        setContentView(R.layout.slidinglayout);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setHomeButtonEnabled(true);
        }
        toolbar.setNavigationIcon(R.drawable.nav_menu);

        LinearLayout menuLayout = findViewById(R.id.menu);
        SlidingPaneLayout slidingPaneLayout = findViewById(R.id.slidingpanelayout);
        slidingPaneLayout.setSliderFadeColor(Color.parseColor("#55808080"));
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

        toolbar.setNavigationOnClickListener(v -> slidingPaneLayout.openPane());

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(v -> showBottomSheetDialog());

        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) slidingPaneLayout.getChildAt(0).getLayoutParams();
        int maxMargin = lp.getMarginEnd() / 2;
        SlidingPaneLayout.LayoutParams contentParams = (SlidingPaneLayout.LayoutParams) menuLayout
                .getLayoutParams();

        slidingPaneLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(@NonNull View panel, float slideOffset) {
                float sc = Math.abs(1 - slideOffset);
                fab.setScaleX(sc);
                fab.setScaleY(sc);

                int contentMargin = (int) (slideOffset * maxMargin);
                contentParams.setMargins(0, contentMargin, 0, contentMargin);
                menuLayout.setLayoutParams(contentParams);
                float scale = 1 - ((1 - slideOffset) * maxMargin * 2)
                        / (float) displayMetrics.heightPixels;

                menuLayout.setScaleX(scale);
                menuLayout.setScaleY(scale);
                menuLayout.setPivotX(0);
                menuLayout.setPivotY(displayMetrics.heightPixels / 2f);

            }

            @Override
            public void onPanelOpened(@NonNull View panel) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    window.getDecorView().setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }
            }

            @Override
            public void onPanelClosed(@NonNull View panel) {
                window.getDecorView().setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
        });
    }

    private void showBottomSheetDialog() {
        BottomSheetDialog dialog = new BottomSheetDialog(this);
        View dialogView = View.inflate(this, R.layout.sheetdialog, null);
        dialog.setContentView(dialogView);
        View v = dialog.getDelegate().findViewById(R.id.design_bottom_sheet);
        if (v != null) {
            v.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        }
        dialog.show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        log("onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        log("onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        log("onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        log("onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        log("onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        log("onRestart");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        log("onNewIntent");
    }

    private void log(String p1) {
        Log.i(TAG, "-----" + p1 + "------");
    }
}
